<?php
require_once dirname(__FILE__) . '/../config/config.php';
class UserController
{
    private $smarty;
    public function __construct()
    {
    }

    public function main(&$smarty)
    {
        $this->smarty = $smarty;
    }

    public function loginAction()
    {
        $userService = new UserService();
        $context = new BaseContext();
        $result = new ServiceResult();

        if(isset($_POST) && !empty($_POST))
        {
            $uid = $_POST['uid'];
            $pass = $_POST['pass'];

            $rowUser = $userService->login($context, $result, $uid, $pass);

            if ($rowUser == false)
            {
                $this->smarty->display('login.tpl');
            }
            else
            {
                $_SESSION['login'] = $rowUser;
                $this->smarty->assign('session',$_SESSION['login']);
                $_SESSION['time_out'] = time();
                setcookie("token", "value", time()+60*60*24*100, "/");
                header("location:".WEB_BASE_URL."/index.php");
            }
        }

        else {
            $this->smarty->display('login.tpl');
        }
    }

    public function logoutAction()
    {
        unset($_SESSION['login']);
        header("location:".WEB_BASE_URL."/index.php?controller=user&action=login");
    }
}

?>